/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.open.cloud.common.utils;

import com.open.cloud.common.base.Callable;
import com.open.cloud.common.utils.spring.SpringApplicationContext;
import lombok.val;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Leijian
 */
public class PropertyUtils {
    public static String NULL = "<?NULL?>";

    public static void eachProperty(Callable.Action3<String, String, Object> call) {
        for (val key : System.getProperties().stringPropertyNames()) {
            call.invoke("properties", key, System.getProperty(key));
        }
        for (val kv : System.getenv().entrySet()) {
            call.invoke("env", kv.getKey(), kv.getValue());
        }
    }

    private static <T> T getProperty(String key, T defaultvalue) {
        String value = System.getProperty(key);
        if (value == null) {
            value = System.getenv(key);
        }
        if (value == null && SpringApplicationContext.getApplicationContext() != null) {
            value = SpringApplicationContext.getApplicationContext().getEnvironment()
                .getProperty(key);
        }
        if (value == null) {
            return defaultvalue;
        }
        return (T) ConvertUtils.convert(value, defaultvalue.getClass());
    }

    public static Object getProperty(String key) {
        String value = System.getProperty(key);
        if (value == null) {
            value = System.getenv(key);
        }
        if (value == null && SpringApplicationContext.getApplicationContext() != null) {
            value = SpringApplicationContext.getApplicationContext().getEnvironment()
                .getProperty(key);
        }
        return value;
    }

    public static <T> T getEnvProperty(String key, T defaultvalue) {
        String value = System.getenv(key);
        if (value == null) {
            return defaultvalue;
        } else {
            return (T) ConvertUtils.convert(value, defaultvalue.getClass());
        }
    }

    public static <T> T getSystemProperty(String key, T defaultvalue) {
        String value = System.getProperty(key);
        if (value == null) {
            return defaultvalue;
        } else {
            return (T) ConvertUtils.convert(value, defaultvalue.getClass());
        }
    }

    public static void setDefaultInitProperty(Class cls, String module, String key,
                                              String propertyValue) {
        setDefaultInitProperty(cls, module, key, propertyValue, "");
    }

    public static void setDefaultInitProperty(Class cls, String module, String key,
                                              String propertyValue, String message) {
        if (StringUtils.isEmpty(PropertyUtils.getPropertyCache(key, ""))) {
            if (!StringUtils.isEmpty(propertyValue)) {
                System.setProperty(key, propertyValue);
                //PropertyCache.Default.tryUpdateCache(key, propertyValue);
                LogUtils.info(cls, module, "设置" + key + "=" + propertyValue + " " + message);
            }
        }
    }

    public static <T> T getPropertyCache(String key, T defaultvalue) {
        //return PropertyCache.Default.get(key, defaultvalue);
        return null;
    }

}
